package net.yeah.mickey_zhouyou.database.util;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map.Entry;

import net.yeah.mickey_zhouyou.database.domain.BaseRelationship;

public class GenericReflectionUtil {

	@SuppressWarnings("unchecked")
	public static <R extends BaseRelationship<?, ?>> Entry<Class<?>, Class<?>> getRelationshipGenericTypes(
			Class<R> clazz) {
		if (clazz == null)
			return null;

		Class<?> curClass = clazz;
		while (curClass.getSuperclass() != BaseRelationship.class) {
			curClass = curClass.getSuperclass();
		}

		final Type[] classPair = ((ParameterizedType) curClass
				.getGenericSuperclass()).getActualTypeArguments();

		Entry<?, ?> entry = MapUtil.makeUnmodifiableEntry(classPair[0],
				classPair[1]);
		return (Entry<Class<?>, Class<?>>) entry;
	}

}
